﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace SobFS.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Total Memory: {0}K", ((double)(GC.GetTotalMemory(false) / 1024)));
            Console.Read();

            string e = Encrypt3DES("999999999", "#s^un2ye31<cn%|aoXpR,+vh");
            Console.WriteLine(e);

            Console.WriteLine();
            string d = Decrypt3DES(e, "999999999");
            Console.WriteLine(d);

            Console.Read();

        }

        public static string Encrypt3DES(string a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESEncrypt = DES.CreateEncryptor();
            byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
            return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }

        public static string Decrypt3DES(string a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte[] tdesKey = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(a_strKey));
            DES.Key = tdesKey;
            //DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
            DES.Mode = CipherMode.ECB;
            DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ICryptoTransform DESDecrypt = DES.CreateDecryptor();
            string result = "";
            try
            {
                byte[] Buffer = Convert.FromBase64String(a_strString);
                result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }
            catch (Exception e)
            {
            }
            return result;
        }


    }
}
